var prefix = "/itm/project"
$(function() {
	loadType();
	load();
	laydate({
		elem : '#gmtCreateStr'
	});
	laydate({
		elem : '#gmtCreateend'
	});
	laydate({
		elem : '#gmtModifiedend'
	});
	laydate({
		elem : '#gmtModifiedStr'
	});
});

function load() {
	$('#exampleTable')
			.bootstrapTable(
					{
						method : 'get', // 服务器数据的请求方式 get or post
						url : prefix + "/list", // 服务器数据的加载地址
						iconSize : 'outline',
						toolbar : '#exampleToolbar',
						striped : true, // 设置为true会有隔行变色效果
						dataType : "json", // 服务器返回的数据类型
						pagination : true, // 设置为true会在底部显示分页条
						singleSelect : false, // 设置为true将禁止多选
						pageSize : 10, // 如果设置了分页，每页数据条数
						pageNumber : 1, // 如果设置了分布，首页页码
						showColumns : false, // 是否显示内容下拉框（选择显示的列）
						sidePagination : "server", // 设置在哪里进行分页，可选值为"client" 或者 "server"
						queryParams : function(params) {
							return {
								limit: params.limit,
								offset:params.offset,
					            name:$('#searchName').val(),
					           	status:$('#status').val(),
								gmtCreateStr:$('#gmtCreateStr').val(),
								gmtCreateend:$('#gmtCreateend').val(),
								gmtModifiedend:$('#gmtModifiedend').val(),
								gmtModifiedStr:$('#gmtModifiedStr').val()
							};
						},
						columns : [
								{
									checkbox : true
								},
								{
									visible : false,
									field : 'id', 
									title : '主键' 
								},
								{
									field : 'name', 
									title : '项目名称'
								},
								{
									field : 'itmNumber', 
									title : '项目编号',
									formatter: function(value,row,index){
										// if(row.ismanager==1){
										// 	return '<a href="/itm/project/ckinfo/'+row.id+'" target="_blank">'+row.itmNumber+'</a>';
										// }else{
										// 	return '<a href="/itm/project/ckedit/'+row.id+'" target="_blank">'+row.itmNumber+'</a>';
										// }
										return '<a href="/itm/project/ckinfo/'+row.id+'" target="_blank">'+row.itmNumber+'</a>';
									}
								},
								{
									visible : false,
									field : 'itmProTypeid', 
									title : '项目类型ID' 
								},
								{
									field : 'itmProType', 
									title : '项目类型' 
								},
								{
									visible : false,
									field : 'itmManagerId', 
									title : '项目负责人ID' 
								},
								{
									field : 'itmManagerName', 
									title : '项目负责人' 
								},
								{
									visible : false,
									field : 'deptId', 
									title : '业务部门ID' 
								},
								{
									field : 'deptName', 
									title : '业务部门' 
								},
								{
									visible : false,
									field : 'itmSource', 
									title : '项目来源',
									formatter : function(value, row, index){
										if(value==1){
											return '<span class="label label-primary">老客户介绍</span>';
										}else if(value==2){
											return '<span class="label label-primary">招标要求</span>';
										}else if(value==3){
											return '<span class="label label-primary">来电咨询</span>';
										}else if(value==4){
											return '<span class="label label-primary">其他渠道</span>';
										}
									}
								},
								{
									visible : false,
									field : 'itmModel', 
									title : '项目模式',
									formatter : function(value, row, index){
										if(value==1){
											return '<span class="label label-primary">设备</span>';
										}else if(value==2){
											return '<span class="label label-primary">运营</span>';
										}
									}
								},
								{
									visible : false,
									field : 'itmAddress', 
									title : '项目地址' 
								},
								{
									visible : false,
									field : 'itmDecSite', 
									title : '拟承接装修部位' 
								},
								{
									field : "projectAmount",
									title : "总金额",
									formatter: function(value,row,index){
										// if(row.ismanager==1){
										// 	var projectAmount =parseFloat(row.projectAmount).toFixed(2);
										// 	return projectAmount;
										// }else{
										// 	return 'NAN';
										// }
										var projectAmount =parseFloat(row.projectAmount).toFixed(2);
										return projectAmount;
									}
								},
								{
									field : 'receivedAmount',
									title : '已收金额',
									formatter: function(value,row,index){
										var receivedAmount =parseFloat(row.receivedAmount).toFixed(2);
										return '<a href="/fin/receipt/getRecpidByitm/'+row.id+'" target="_blank">'+receivedAmount+'</a>';
										// if(row.ismanager==1){
										// 	var receivedAmount =parseFloat(row.receivedAmount).toFixed(2);
										// 	return '<a href="/fin/receipt/getRecpidByitm/'+row.id+'" target="_blank">'+receivedAmount+'</a>';
										// }else{
										// 	return 'NAN';
										// }
									}
								},
								{
									field : 'invoiceAmount',
									title : '开票金额',
									formatter: function(value,row,index){
										// if(row.ismanager==1){
										// 	var invoiceAmount =parseFloat(row.invoiceAmount).toFixed(2);
										// 	return '<a href="/fin/billing/getInvoiceByitm/'+row.id+'" target="_blank">'+invoiceAmount+'</a>';
										// }else{
										// 	return 'NAN';
										// }
										var invoiceAmount =parseFloat(row.invoiceAmount).toFixed(2);
										return '<a href="/fin/billing/getInvoiceByitm/'+row.id+'" target="_blank">'+invoiceAmount+'</a>';
									}
								},
								{
									visible : false,
									field : 'itmRemark',
									title : '项目说明'
								},
								{
									field : 'status', 
									title : '项目阶段',
									formatter : function(value, row, index){
										if(value==1){
											return '<span class="label label-primary">跟踪阶段</span>';
										}else if(value==2){
											return '<span class="label label-primary">商务阶段</span>';
										}else if(value==3){
											return '<span class="label label-primary">在建阶段</span>';
										}else if(value==4){
											return '<span class="label label-primary">维保阶段</span>';
										}else if(value==5){
											return '<span class="label label-primary">完成阶段</span>';
										}else if(value==8){
											return '<span class="label label-danger">终止项目</span>';
										}
									}
								},
								{
									field : 'custName', 
									title : '客户名称' ,
									formatter: function(value,row,index){
										return '<a href="/itm/project/itmcustName/'+row.custName+'" target="_blank">'+row.custName+'</a>';
									}
								},
								{
									field : 'gmtCreate',
									title : '创建时间'
								},
								{
									title : '操作',
									field : 'operation',
									align : 'center',
									formatter : function(value, row, index) {
										var exp = '<a class="btn btn-sm" href="/itm/project/exportinfo/'+row.id+'"  title="导出">' +
											'<i class="fa fa-reply"></i></a> ';
										if(row.status == 1){
											var e = '<a class="btn btn-primary btn-sm '+s_edit_h+'" href="#" mce_href="#" title="编辑" onclick="edit(\''
												+ row.id
												+ '\')"><i class="fa fa-edit"></i></a> ';
											var d = '<a class="btn btn-warning btn-sm" href="#" title="终止"  mce_href="#" onclick="remove(\''
												+ row.id
												+ '\')"><i class="fa fa-remove"></i></a> ';
											var f = '<a class="btn btn-info btn-sm " href="#" title="上传文件"  mce_href="#" onclick="uploadInfo(\''
												+ row.id
												+ '\')"><i class="fa fa-upload"></i></a> ';
											return e + d + f+exp;
										}else if(row.status==2){
											var f = '<a class="btn btn-primary btn-sm '+s_edit_h+'" href="#" mce_href="#" title="商务完善" onclick="bissedit(\''
												+ row.id
												+ '\')"><i class="fa fa-edit">商务完善</i></a>';
											var d = '<a class="btn btn-info btn-sm " href="#" title="上传文件"  mce_href="#" onclick="uploadInfo(\''
												+ row.id
												+ '\')"><i class="fa fa-upload"></i></a> ';
											return f+d+exp;
										}else if(row.status==3){
											var f = '<a class="btn btn-primary btn-sm '+s_edit_h+'" href="#" mce_href="#" title="正式信息" onclick="bissedittwo(\''
												+ row.id
												+ '\')"><i class="fa fa-edit">正式信息</i></a>';
											var e = '<a class="btn btn-primary btn-sm '+s_edit_h+'" href="#" mce_href="#" title="完成建设" onclick="inprogress(\''
												+ row.id
												+ '\')"><i class="fa fa-edit">完成建设</i></a> ';
											var d = '<a class="btn btn-info btn-sm " href="#" title="上传文件"  mce_href="#" onclick="uploadInfo(\''
												+ row.id
												+ '\')"><i class="fa fa-upload"></i></a> ';
											return f+e+d+exp;
										}else if(row.status==4){
											var e = '<a class="btn btn-primary btn-sm '+s_edit_h+'" href="#" mce_href="#" title="完成维保" onclick="mainten(\''
												+ row.id
												+ '\')"><i class="fa fa-edit">开始维保</i></a> ';
											var d = '<a class="btn btn-info btn-sm " href="#" title="上传文件"  mce_href="#" onclick="uploadInfo(\''
												+ row.id
												+ '\')"><i class="fa fa-upload"></i></a> ';
											return e+d+exp;
										}
									}
								} ]
					});
}
function reLoad() {
	$('#exampleTable').bootstrapTable('refresh');
}

function exportData() {
	var name=$('#searchName').val();
	var statuse=$('#status').val();
	var gmtCreateStre=$('#gmtCreateStr').val();
	var gmtCreateende=$('#gmtCreateend').val();
	var gmtModifiedende=$('#gmtModifiedend').val();
	var gmtModifiedStre=$('#gmtModifiedStr').val();
	var url =prefix + '/exportData';
	if(name != null){
		if (url.indexOf('?') > -1) {
			url += '&name=' + name;
		} else {
			url += '?name=' + name;
		}
	}
	if(statuse != null){
		if (url.indexOf('?') > -1) {
			url += '&statuse=' + statuse;
		} else {
			url += '?statuse=' + statuse;
		}
	}
	if(gmtCreateStre != null){
		if (url.indexOf('?') > -1) {
			url += '&gmtCreateStre=' + gmtCreateStre;
		} else {
			url += '?gmtCreateStre=' + gmtCreateStre;
		}
	}
	if(gmtCreateende != null){
		if (url.indexOf('?') > -1) {
			url += '&gmtCreateende=' + gmtCreateende;
		} else {
			url += '?gmtCreateende=' + gmtCreateende;
		}
	}
	if(gmtModifiedende != null){
		if (url.indexOf('?') > -1) {
			url += '&gmtModifiedende=' + gmtModifiedende;
		} else {
			url += '?gmtModifiedende=' + gmtModifiedende;
		}
	}
	if(gmtModifiedStre != null){
		if (url.indexOf('?') > -1) {
			url += '&gmtModifiedStre=' + gmtModifiedStre;
		} else {
			url += '?gmtModifiedStre=' + gmtModifiedStre;
		}
	}
	console.log(url);
	var form = document.createElement('form');
	form.setAttribute('action', url);
	form.setAttribute('method', 'post');
	form.setAttribute('style', 'display:none');
	document.body.appendChild(form);
	form.submit();
	document.body.removeChild(form)
}

function add() {
	var index = layer.open({
		type : 2,
		title : '增加',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix + '/add' // iframe的url
	});
	layer.full(index);
}
function edit(id) {
	var index = layer.open({
		type : 2,
		title : '编辑',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix + '/edit/' + id // iframe的url
	});
	layer.full(index);
}
function remove(id) {
	$.ajax({
		url : "/fin/reim/getSalExpprice/"+id,
		success : function(r) {
			console.log(r);
			var salExprice = r;
			console.log(salExprice);
			if(salExprice>0){
				console.log("11111");
				moreremove(id,salExprice);
			}else{
				console.log("22222");
				lessremove(id);
			}
		}
	});
}


function moreremove(id,salExprice) {
	layer.confirm('该项目下还有['+salExprice+']报销金额，确定要终止该项目吗？如果确定终止，' +
		'确定报销金额是否需要转移到其他项目。', {
		btn : [ '确定', '不转移' ]
	}, function() {
		layer.open({
			type : 2,
			title : '终止项目',
			maxmin : true,
			shadeClose : false, // 点击遮罩关闭层
			area : [ '800px', '520px' ],
			content : prefix + '/moreremove/'+id+'?salexpprice='+salExprice// iframe的url
		});
		var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
		parent.layer.close(index);
	},function () {
		layer.confirm('确定要终止该项目吗？', {
			btn : [ '确定', '取消' ]
		}, function() {
			$.ajax({
				url : prefix+"/remove",
				type : "post",
				data : {
					'id' : id
				},
				success : function(r) {
					if (r.code==0) {
						layer.msg(r.msg);
						reLoad();
					}else{
						layer.msg(r.msg);
					}
				}
			});
		})
	})
}


function lessremove(id) {
	layer.confirm('确定要终止该项目吗？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/remove",
			type : "post",
			data : {
				'id' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					reLoad();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}

function batchRemove() {
	var rows = $('#exampleTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
	if (rows.length == 0) {
		layer.msg("请选择要删除的数据");
		return;
	}
	layer.confirm("确认要删除选中的'" + rows.length + "'条数据吗?", {
		btn : [ '确定', '取消' ]
	// 按钮
	}, function() {
		var ids = new Array();
		// 遍历所有选择的行数据，取每条数据对应的ID
		$.each(rows, function(i, row) {
			ids[i] = row['id'];
		});
		$.ajax({
			type : 'POST',
			data : {
				"ids" : ids
			},
			url : prefix + '/batchRemove',
			success : function(r) {
				if (r.code == 0) {
					layer.msg(r.msg);
					reLoad();
				} else {
					layer.msg(r.msg);
				}
			}
		});
	}, function() {

	});
}

function loadType(){
	var html = "";
	$.ajax({
		url : '/common/dict/list/itm_project',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].value + '">' + data[i].name + '</option>'
			}
			$(".chosen-select").append(html);
			$(".chosen-select").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}

function bissedit(id) {
	var page = layer.open({
		type : 2,
		title : '商务信息',
		maxmin : true,
		shadeClose : false,
		area : [ '800px', '520px' ],
		content : prefix + '/bissedit/' + id
	});
	layer.full(page);
}

function bissedittwo(id) {
	var page = layer.open({
		type : 2,
		title : '商务信息',
		maxmin : true,
		shadeClose : false,
		area : [ '800px', '520px' ],
		content : prefix + '/bissedittwo/' + id
	});
	layer.full(page);
}

function contractconf(id) {
	var page = layer.open({
		type : 2,
		title : '商务信息',
		maxmin : true,
		shadeClose : false,
		area : [ '800px', '520px' ],
		content : prefix + '/contractconf/' + id
	});
	layer.full(page);
}

function inprogress(id) {
	layer.confirm('确定该项目建设完成吗？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/inprogress",
			type : "post",
			data : {
				'id' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					reLoad();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}

function mainten(id) {
	layer.open({
		type : 2,
		title : '编辑',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix + '/mainten/' + id // iframe的url
	});
}


function uploadInfo(id) {
	var index = layer.open({
		type : 2,
		title : '上传文件',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix + '/uploadInfo/' + id // iframe的url
	});
	layer.full(index);
}